package edu.dayu.util;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

public class JsonToSQL {
    public static void main(String[] args) {
        String filePath = "F:\\BaiduNetdiskDownload\\医疗项目--尚医通\\资料\\资料\\05-医院接口模拟系统\\示例数据\\department.json";
        System.out.println(readFile(filePath));
    }
     public static int i =0;
    /**
     * 读取txt文件，返回json字符串
     * @param filePath
     * @return
     */
    public static String readFile(String filePath){
        List<String> list = new ArrayList<>();
        Map<String,String> map = new LinkedHashMap<>();
        try {
            FileReader fileReader = new FileReader(filePath);
            BufferedReader reader = new BufferedReader(fileReader);
            FileWriter fileWriter = new FileWriter("C:\\Users\\86134\\Desktop\\test.txt");
            BufferedWriter writer = new BufferedWriter(fileWriter);
            String line;
            while((line = reader.readLine()) != null){
                if (!"[".equals(line)&&!"]".equals(line))
                list.add(transfer(line));
            }
            writer.write(list.toString());
            reader.close();
            writer.close();
        }catch (IOException e){
            System.out.println(i);
            e.printStackTrace();
        }
        return list.toString();
    }

    /**
     * 将一行数据转化为json字符串
     * @param line
     * @return
     */
    public static String transfer(String line){
        String[] split = line.split(",");
        StringBuffer str = new StringBuffer();
        str.append("(");
        for (int i = 0; i < split.length; i++) {
            if (split[i]==null||split[i].charAt(0)=='[')
                continue;
            String[] strings = split[i].split(":");
            str.append(strings[1]+",");
        }
        str = new StringBuffer(str.substring(0,str.length()-2));
        str.append(")");
        i++;
        return str.toString();
    }
}
